package com.lbt.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lbt.commonuntils.JwtUtils;
import com.lbt.commonuntils.R;
import com.lbt.eduorder.entity.Order;
import com.lbt.eduorder.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-09
 */
@Api(description = "订单管理")
@RestController
//@CrossOrigin
@RequestMapping("/eduorder/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    @ApiOperation("生成订单")
    @PostMapping("createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest request){
//        System.out.println("根据请求头查询ID*******************************************");
        String userid = JwtUtils.getMemberIdByJwtToken(request);
//        System.out.println(" 查询对象*******************************************");
  //      System.out.println(userid);
        String orderId = orderService.createOrder(courseId,userid);
        return R.ok().data("orderId",orderId);
    }
    @ApiOperation("查询订单信息")
    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable("orderId") String orderId){
        QueryWrapper<Order>wrapper=new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        Order order = orderService.getOne(wrapper);
        //调试
      //  System.out.println("查询订单信息："+order.toString());
        return R.ok().data("item",order);
    }
    //根据客程ID  用户ID 查询课程的支付状态
    @ApiOperation("根据客程ID  用户ID 查询课程的支付状态")
    @GetMapping("isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@PathVariable("courseId") String courseId,@PathVariable("memberId") String memberId){
      //  String tokenId = JwtUtils.getMemberIdByJwtToken(request);   //得到用户ID
        QueryWrapper<Order>wrapper=new QueryWrapper<>();
        wrapper.eq("member_id",memberId);
        wrapper.eq("course_id",courseId);
        wrapper.eq("status",1);
        int count = orderService.count(wrapper);
        if(count<=0){
            return false;
        }
        return true;
    }

}

